package com.danke.lib.pay;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import org.json.JSONObject;

/**
 * @author wupuquan
 * @version 1.0
 * @since 2019/1/8 14:22
 */
public class DKPayWeb {

    protected DKPayActivity mActivity;
    private View contentView;
    private LinearLayout mLlBack;
    protected DKPayWebView mWebView;
    protected ProgressBar mProgressBar;

    @SuppressLint("InflateParams")
    public DKPayWeb(DKPayActivity activity, JSONObject credential) {
        this.mActivity = activity;
        contentView = LayoutInflater.from(activity).inflate(R.layout.dk_pay_activity, null);
        mWebView = contentView.findViewById(R.id.webView);
        mProgressBar = contentView.findViewById(R.id.progressBar);
        mLlBack = contentView.findViewById(R.id.ll_back);
        mLlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mActivity != null) {
                    mActivity.onBackPressed();
                }
            }
        });

        init();
        handleData(credential);
    }

    protected void init() {
        if (mWebView == null) {
            return;
        }
        mWebView.setWebChromeClient(new DKPayWebView.DKPayWebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (mProgressBar != null) {
                    if (newProgress >= 100) {
                        mProgressBar.setVisibility(View.GONE);
                    } else {
                        mProgressBar.setVisibility(View.VISIBLE);
                        mProgressBar.setProgress(newProgress);
                    }
                }
            }
        });
        mWebView.setWebViewClient(new DKPayWebView.DKPayWebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                if (mActivity != null) {
                    mActivity.setResult(DKPayActivity.CANCEL);
                }
            }
        });
    }

    protected void handleData(JSONObject charge) {

    }

    protected void load(String url, byte[] data) {
        if (mActivity != null) {
            mActivity.setContentView(contentView);
        }
        if (mWebView != null && !TextUtils.isEmpty(url)) {
            if (data == null) {
                mWebView.loadUrl(url);
            } else {
                mWebView.postUrl(url, data);
            }
        }
    }

    public boolean canGoBack() {
        return mWebView != null && mWebView.canGoBack();
    }

    public void goBack() {
        if (mWebView != null) {
            mWebView.goBack();
        }
    }

    public void onResume() {
        if (mWebView != null) {
            mWebView.onResume();
        }
    }

    public void onPause() {
        if (mWebView != null) {
            mWebView.onPause();
        }
    }

    public void onDestroy() {
        if (mWebView != null) {
            mWebView.destroy();
        }
    }
}
